package com.tvserials.notification.adapters;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.tvserials.notification.R;
import com.tvserials.notification.utils.ResourceUtils;
import com.tvserials.notification.dto.Episode;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Alex on 26.03.2014.
 */
public class TvEpisodeListAdapter extends ArrayAdapter<Episode> {
    private List<Episode> list;

    //this custom adapter receives an ArrayList of RowData objects.
    //RowData is my class that represents the data for a single row and could be anything.
    public TvEpisodeListAdapter(Context context, int textViewResourceId, List<Episode> rowDataList) {
        //populate the local list with data.
        super(context, textViewResourceId, rowDataList);
        this.list = new ArrayList<Episode>();
        if (rowDataList != null && !rowDataList.isEmpty()) {
            this.list.addAll(rowDataList);
        }
    }

    public Episode getItemByPosition(int position) {
        return list.get(position);
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            //creating LayoutInflator for inflating the row layout.
            LayoutInflater inflator = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //inflating the row layout we defined earlier.
            rowView = inflator.inflate(R.layout.chapter_items_list_layout, null);

            //creating the ViewHolder we defined earlier.
            ViewHolder viewHolder = new ViewHolder();
            //setting the views into the ViewHolder.
            viewHolder.title = (TextView) rowView.findViewById(R.id.titleText);
            viewHolder.chapterName = (TextView) rowView.findViewById(R.id.chapterText);
            viewHolder.chapterNo = (TextView) rowView.findViewById(R.id.chapterNoText);
            viewHolder.dateText = (TextView) rowView.findViewById(R.id.dateText);
            viewHolder.image = (ImageView) rowView.findViewById(R.id.icon);
//            viewHolder.details = (ImageView) rowView.findViewById(R.id.details);
            viewHolder.sourceIcon = (ImageView) rowView.findViewById(R.id.sourceIcon);
            rowView.setTag(viewHolder);
        }
        if (!list.isEmpty() && list.size() > position) {
            //setting data into the the ViewHolder.
            ViewHolder holder = (ViewHolder) rowView.getTag();
            Episode episodeVO = list.get(position);
            byte[] image = episodeVO.getImage();
            if (image != null) {
                holder.image.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
            }
            holder.chapterName.setText(episodeVO.getChapterName());
            holder.title.setText(episodeVO.getShowName());
            holder.chapterNo.setText(episodeVO.getChapterNumber());
            holder.dateText.setText(formatDate(episodeVO.getReleaseDate()));
            holder.sourceIcon.setImageResource(ResourceUtils.getIconResourceId(episodeVO.getSourceType()));
//            holder.details.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_details));
        }

        showAnimation(rowView);
        //return the row view.
        return rowView;
    }

    private void showAnimation(View rowView) {
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.fadein);
        rowView.setAnimation(animation);
        animation = null;
    }

    private String formatDate(Date releaseDate) {
        if (releaseDate != null) {
            return DateUtils.formatSameDayTime(releaseDate.getTime(), new Date().getTime(),
                    DateFormat.SHORT, DateFormat.SHORT).toString();
        }
        return "";
    }


    private static class ViewHolder {
        TextView title;
        TextView chapterName;
        ImageView image;
        TextView chapterNo;
        TextView dateText;
        ImageView details;
        ImageView sourceIcon;
    }


}
